import * as Function from "./Function";
import Method from "./Method";
import Directive from "./Directive";
import $validator from "./Validator";
import Uderscore from "underscore";
import debounce from "./debounce";
import throttle from "./throttle";
import sy from "@/MUI/utils/WaterMark";

const $m = {
  ...Function,
  ...Method,
  ...Uderscore,
  debounce,
  throttle,
  sy,
};
export { $m, $validator };
export default {
  install: function (Vue) {
    Vue.use(Directive);

    Vue.prototype.$m = $m;
    Vue.prototype.$validator = $validator;

    // // 表单重置
    Vue.prototype.resetForm = function (formName, cb) {
      this.$refs[formName].resetFields();
      if (cb) {
        cb();
      }
    };

    // 触发elementui 表单校验
    Vue.prototype.ElDispatch = function (componentName, eventName, params) {
      var parent = this.$parent || this.$root;
      var name = parent.$options.componentName;

      while (parent && (!name || name !== componentName)) {
        parent = parent.$parent;

        if (parent) {
          name = parent.$options.componentName;
        }
      }
      if (parent) {
        parent.$emit.apply(parent, [eventName].concat(params));
      }
    };

    Vue.prototype.tableIndex = function (index, page, pageSize) {
      return index + 1 + (page - 1) * pageSize;
    };

    // Vue.prototype.debounce = Function.debounce

    Vue.prototype.downloadFile = function (url, fileName) {
      var a = document.createElement("a");
      a.target = "_blank";
      a.download = fileName;
      a.href = url;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    };

    // 日期原型方法
    // 时间格式化
    Date.prototype.F = function (fmt) {
      //author: meizz
      var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        S: this.getMilliseconds(), //毫秒
      };
      if (/(y+)/.test(fmt))
        fmt = fmt.replace(
          RegExp.$1,
          (this.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length == 1
              ? o[k]
              : ("00" + o[k]).substr(("" + o[k]).length)
          );
      return fmt;
    };

    String.prototype.trim = function () {
      return this.replace(/^(\s*)|(\s*)$/g, "");
    };
  },
};
